﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;


namespace 应用程序域动态库１
{
    /// <summary>
    /// 代理应用程序域的处理类
    /// </summary>
    public class ProxyAppDomain 
    {

        public void CallBack()
        {
            //System.Threading.

            System.Threading.Tasks.TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;

            AppDomain.CurrentDomain.UnhandledException -= tmp_UnhandledException;
            AppDomain.CurrentDomain.UnhandledException += tmp_UnhandledException;
        }

        void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            e.SetObserved();
            System.Threading.SynchronizationContext.Current.Post((obj) =>
            {
                Console.WriteLine(e.Exception + string.Empty);
            }, null); 
        }
        /// <summary>
        /// 异常隔离 
        /// http://www.cnblogs.com/huihui0630/archive/2010/06/30/1768387.html
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void tmp_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {

            //System.Windows.Application.Current.ex
           

            Console.WriteLine("引起一个末处理的异常.........", e.ExceptionObject);


        }


        /// <summary>
        /// 引发一个异常
        /// </summary>
        public void Error()
        {
             
           //System.Threading.ThreadPool.QueueUserWorkItem((obj) =>
           // {

                string s = System.Threading.Thread.GetDomain().FriendlyName;
                System.Threading.Thread.Sleep(100);

                throw new NotSupportedException();
            //});

            

        }

    }
}
